/**
 *
 */
package com.zhuoyue.zhtj.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.zhtj.entity.Interthing;
import com.zhuoyue.zhtj.service.InterthingService;

/**
 * 内部物品管理Controller
 * @author hss
 * @version 2017-09-18
 */
@Controller
@RequestMapping(value = "${adminPath}/zhtj/interthing")
public class InterthingController extends BaseController {

	@Autowired
	private InterthingService interthingService;
	
	@ModelAttribute
	public Interthing get(@RequestParam(required=false) String id) {
		Interthing entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = interthingService.get(id);
		}
		if (entity == null){
			entity = new Interthing();
		}
		return entity;
	}
	
	@RequiresPermissions("zhtj:interthing:view")
	@RequestMapping(value = {"list", ""})
	public String list(Interthing interthing, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Interthing> page = interthingService.findPage(new Page<Interthing>(request, response), interthing); 
		model.addAttribute("page", page);
		return "zhuoyue/zhtj/interthingList";
	}

	@RequiresPermissions("zhtj:interthing:view")
	@RequestMapping(value = "form")
	public String form(Interthing interthing, Model model) {
		model.addAttribute("interthing", interthing);
		return "zhuoyue/zhtj/interthingForm";
	}

	@RequiresPermissions("zhtj:interthing:edit")
	@RequestMapping(value = "save")
	public String save(Interthing interthing, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, interthing)){
			return form(interthing, model);
		}
		if(interthing.getIsNewRecord()){
			if(interthingService.findByNum(interthing)){
			addMessage(redirectAttributes, "该生已领取过");
			return "redirect:"+Global.getAdminPath()+"/zhtj/interthing/?repage";
			}
		}
		interthingService.save(interthing);
		addMessage(redirectAttributes, "保存物品记录成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/interthing/?repage";
	}
	
	@RequiresPermissions("zhtj:interthing:edit")
	@RequestMapping(value = "delete")
	public String delete(Interthing interthing, RedirectAttributes redirectAttributes) {
		interthingService.delete(interthing);
		addMessage(redirectAttributes, "删除物品记录成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/interthing/?repage";
	}

}